using MediatR;
using Microsoft.AspNetCore.Mvc;
using S.O.Api.Write.Application.Command.Medical;
using S.O.Domain.Dto.Output;
using S.O.ErrorCode;

namespace S.O.Api.Write.Controllers
{
    /// <summary>
    /// 医疗相关写操作API控制器
    /// 提供患者挂号、挂号信息修改、取消挂号等医疗业务相关的写操作API接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MedicalController : ControllerBase
    {
        /// <summary>
        /// MediatR中介者模式服务，用于处理命令
        /// </summary>
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数，通过依赖注入初始化MediatR服务
        /// </summary>
        /// <param name="mediator">MediatR中介者服务</param>
        public MedicalController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 创建挂号记录
        /// 实现患者门诊挂号功能，支持科室选择、医生预约、费用计算等完整流程
        /// </summary>
        /// <param name="request">创建挂号记录请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>挂号单号</returns>
        /// <response code="200">挂号成功，返回挂号单号</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<string>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<string>> CreateRegistration(
            CreateRegistrationCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 更新挂号记录
        /// 支持患者修改挂号信息，如更换医生、调整就诊时间等
        /// </summary>
        /// <param name="request">更新挂号记录请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>更新结果</returns>
        /// <response code="200">更新成功</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="404">挂号记录不存在</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPut]
        [ProducesResponseType(typeof(APIResult<bool>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(404)]
        [ProducesResponseType(500)]
        public Task<APIResult<bool>> UpdateRegistration(
            UpdateRegistrationCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 取消挂号记录
        /// 支持患者取消已预约的挂号，释放医生时间资源
        /// </summary>
        /// <param name="request">取消挂号记录请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>取消结果</returns>
        /// <response code="200">取消成功</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="404">挂号记录不存在</response>
        /// <response code="500">服务器内部错误</response>
        [HttpDelete]
        [ProducesResponseType(typeof(APIResult<bool>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(404)]
        [ProducesResponseType(500)]
        public Task<APIResult<bool>> CancelRegistration(
            CancelRegistrationCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 创建患者信息（医疗卡办理）
        /// 实现患者医疗卡办理功能，包括患者基本信息录入、医疗卡号生成等
        /// </summary>
        /// <param name="request">创建患者信息请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>患者ID</returns>
        /// <response code="200">办卡成功，返回患者ID</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<long>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<long>> CreatePatient(
            CreatePatientCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 医疗卡办理
        /// 支持新办、补办、换卡、挂失补办等多种办卡类型
        /// </summary>
        /// <param name="request">医疗卡办理请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>医疗卡号</returns>
        /// <response code="200">办卡成功，返回医疗卡号</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<string>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<string>> CreateMedicalCard(
            CreateMedicalCardCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 医疗卡挂失
        /// 用于处理医疗卡的挂失业务，挂失后医疗卡将无法使用
        /// </summary>
        /// <param name="request">医疗卡挂失请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>挂失结果</returns>
        /// <response code="200">挂失成功</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<bool>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<bool>> LostMedicalCard(
            LostMedicalCardCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 医疗卡激活
        /// 用于激活新办理的医疗卡，激活后医疗卡才能正常使用
        /// </summary>
        /// <param name="request">医疗卡激活请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>激活结果</returns>
        /// <response code="200">激活成功</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<bool>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<bool>> ActivateMedicalCard(
            ActivateMedicalCardCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }



        /// <summary>
        /// 根据挂号ID更新挂号状态
        /// 主要供医生端使用，用于更新患者就诊状态
        /// </summary>
        /// <param name="id">挂号记录ID</param>
        /// <param name="status">新状态（待诊、就诊中、已完成）</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>更新结果</returns>
        /// <response code="200">状态更新成功</response>
        /// <response code="400">请求参数错误</response>
        /// <response code="404">挂号记录不存在</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPatch("{id}/status")]
        [ProducesResponseType(typeof(APIResult<bool>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(404)]
        [ProducesResponseType(500)]
        public Task<APIResult<bool>> UpdateRegistrationStatus(
            long id,
            [FromBody] string status,
            CancellationToken cancellationToken = default)
        {
            var request = new UpdateRegistrationCommand
            {
                Id = id,
                Status = status
            };
            
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 创建病历记录
        /// 医生为患者创建电子病历，包含主诉、现病史、既往史、体格检查、诊断和治疗建议等完整信息
        /// </summary>
        /// <param name="request">创建病历记录请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>病历记录ID</returns>
        /// <response code="200">病历创建成功，返回病历记录ID</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<string>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<string>> CreateMedicalRecord(
            CreateMedicalRecordCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 创建中药处方
        /// 医生为患者开具中药处方，包含诊断信息和中药明细
        /// </summary>
        /// <param name="request">创建中药处方请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处方ID</returns>
        /// <response code="200">中药处方开具成功，返回处方ID</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<string>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<string>> CreateChineseMedicinePrescription(
            CreateChineseMedicinePrescriptionCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 创建西药处方
        /// 医生为患者开具西药处方，包含诊断信息和西药明细
        /// </summary>
        /// <param name="request">创建西药处方请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处方ID</returns>
        /// <response code="200">西药处方开具成功，返回处方ID</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<string>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        public Task<APIResult<string>> CreateWesternMedicinePrescription(
            CreateWesternMedicinePrescriptionCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 创建收费记录
        /// 用于门诊收费处理，包括药品费用结算和付费状态更新
        /// </summary>
        /// <param name="request">创建收费记录请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>收费单号</returns>
        /// <response code="200">收费成功，返回收费单号</response>
        /// <response code="400">请求参数错误或业务规则验证失败</response>
        /// <response code="404">挂号记录不存在</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost]
        [ProducesResponseType(typeof(APIResult<string>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(404)]
        [ProducesResponseType(500)]
        public Task<APIResult<string>> CreateCharge(
            CreateChargeCommand request,
            CancellationToken cancellationToken = default)
        {
            return _mediator.Send(request, cancellationToken);
        }
    }
}